﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using PServiceBus.Services.Gateway.Runtime;

namespace PServiceBus.Workflow.Activities {

    public sealed class PublishESBMessage<TMessage> : CodeActivity where TMessage : class {
        
        [RequiredArgument]
        public InArgument<string> Topic { get; set; }
        [RequiredArgument]
        public InArgument<string> ESBEndpointAddress { get; set; }
        [RequiredArgument]
        public InArgument<TMessage> Message { get; set; }

        protected override void Execute(CodeActivityContext context) {
            var topic = context.GetValue(Topic);
            var endpointAddress = context.GetValue(ESBEndpointAddress);
            var message = context.GetValue<TMessage>(Message);
            ESB.Connect(endpointAddress);
            Services.Gateway.Runtime.Topic.Select(topic).Publish(message);
        }
    }
}
